/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.sensor.model.impl;

import nl.cloudfarming.sensor.model.GeoSensorDataDAO;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import nl.cloudfarming.sensor.model.GeoSensorDataBO;
import nl.cloudfarming.sensor.model.GeoSensorDataFileBO;
import nl.cloudfarming.sensor.model.GeoSensorDataFileDAO;
import org.springframework.stereotype.Repository;

/**
 * @author I. de Vries
 */
@Repository
public class GeoSensorDataDAOImpl implements GeoSensorDataDAO {

    @PersistenceContext
    private EntityManager em;

    @Resource
    private GeoSensorDataFileDAO geoSensorDataFileDAO;

    @Override
    public GeoSensorDataBO newInstance(GeoSensorDataFileBO geoDataSensorFile, float longtitude, float latitude, int fixType, Date utcTime,
            float speed, float course, float sensor, float vi1, float vi2, float r1, float r2, float r3, String appRate) {

        GeoSensorDataBO geoSensorDataBO = new GeoSensorDataBO(geoDataSensorFile, longtitude, latitude, fixType, utcTime,
            speed, course, sensor, vi1, vi2, r1, r2, r3, appRate);

        em.persist(geoSensorDataBO);

        return geoSensorDataBO;
    }

    @Override
    public boolean mergeGeoSensorData(GeoSensorDataBO geoSensorDataBO) {
        boolean fileBOCreated = true;
        if (geoSensorDataBO.getGeoDataSensorFile() != null) {
            GeoSensorDataFileBO orgGsdf = geoSensorDataBO.getGeoDataSensorFile();
            GeoSensorDataFileBO existGsdf = geoSensorDataFileDAO.findByNameChecksum(orgGsdf.getFileName(), orgGsdf.getChecksum());
            if (existGsdf != null) {
                geoSensorDataBO.setGeoDataSensorFile(existGsdf);
                fileBOCreated = false;
            }
            else {
                geoSensorDataFileDAO.mergeGeoSensorDataFile(orgGsdf);
                geoSensorDataBO.setGeoDataSensorFile(orgGsdf);
            }
        }
        em.merge(geoSensorDataBO);
        return fileBOCreated;
    }

    @Override
    public GeoSensorDataBO findById(long id){
        Query query = em.createQuery("select g from GeoSensorDataBO g where g.geoSensorDataId = :id ");
        query.setParameter("id", id);
        return (GeoSensorDataBO) query.getSingleResult();
    }

    @Override
    public List<GeoSensorDataBO> findAllGeoSensorData() {
        Query query = em.createQuery("select g from GeoSensorDataBO g");
        return query.getResultList();
    }

}
